import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

// 添加医生管理相关路由
import DoctorManage from '@/views/NewDoctor/DoctorManage.vue'
import DoctorAudit from '@/views/NewDoctor/DoctorAudit.vue'
import ServiceAudit from '@/views/NewDoctor/ServiceAudit.vue'

// 添加退款申请路由
import RefundManage from '@/views/NewOrder/RefundManage.vue'
// 添加挂号单管理路由
import RegisterManage from '@/views/NewOrder/RegisterManage.vue'
// 添加问诊单管理路由
import ConsultationManage from '@/views/NewOrder/ConsultationManage.vue'
import PrescriptionOrderManage from '@/views/NewOrder/PrescriptionOrderManage.vue'
import MedicineOrderManage from '@/views/NewOrder/MedicineOrderManage.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Login',
      component: () => import('../views/Account/Login.vue'),
    },
    {
      path: '/Menu',
      name: 'Menu',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/Menu.vue'),
      children: [
        {
          path: '/PermissionGet',
          name: 'PermissionGet',
          // route level code-splitting
          // this generates a separate chunk (About.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/Permission/PermissionGet.vue'),
        },
        {
          path: '/RoleGet',
          name: 'RoleGet',
          // route level code-splitting
          // this generates a separate chunk (About.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/Role/RoleGet.vue'),
        },
        {
          path: '/AccountGet',
          name: 'AccountGet',
          // route level code-splitting
          // this generates a separate chunk (About.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/Account/AccountGet.vue'),
        },
        {
          path: '/AppointmentNotice',
          name: 'AppointmentNotice',
          // route level code-splitting
          // this generates a separate chunk (About.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/Register/AppointmentNotice.vue'),
        },
        {
          path: '/home',
          name: 'home',
          component: HomeView,
        },
        {
          path: '/about',
          name: 'about',
          // route level code-splitting
          // this generates a separate chunk (About.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/AboutView.vue'),
        },
        {
          path: '/user-profile',
          name: 'UserProfile',
          component: () => import('../views/Account/UserProfile.vue'),
        },
        {
          path: '/DoctorScheduleView',
          name: 'DoctorScheduleView',
          component: () => import('../views/DoctorSchedule/DoctorScheduleView.vue'),
        },
        {
          path: '/RegisterView',
          name: 'RegisterView',
          component: () => import('../views/Register/RegisterView.vue'),
        },
        {
          path: '/RegisterDetail/:id',
          name: 'RegisterDetail',
          component: () => import('../views/Register/RegisterDetail.vue'),
        },
        {
          path: '/PatientDashboard',
          name: 'PatientDashboard',
          component: () => import('../views/NewFigma/PatientDashboard.vue'),
        },
        {
          path: '/DoctorManage',
          name: 'DoctorManage',
          component: () => import('../views/Doctor/DoctorManage.vue'),
        },
        {
        path: '/DoctorAdd',
        name: 'DoctorAdd',
        component: () => import('../views/Doctor/DoctorAdd.vue'),
      },
        {
          path: '/DepartmentView',
          name: 'DepartmentView',
          component: () => import('../views/Department/DepartmentView.vue'),
        },
        {
          path: '/FirstLevelDepartmentList',
          name: 'FirstLevelDepartmentList',
          component: () => import('../views/Department/FirstLevelDepartmentList.vue'),
        },
        {
          path: '/SecondLevelDepartmentList/:parentId',
          name: 'SecondLevelDepartmentList',
          component: () => import('../views/Department/SecondLevelDepartmentList.vue'),
        },
        {
          path: '/LogView',
          name: 'LogView',
          component: () => import('../views/Department/LogView.vue'),
        },
        {
          path: '/ConsultationView',
          name: 'ConsultationView',
          component: () => import('../views/Order/ConsultationView.vue'),
        },
        {
          path: '/MedicineView',
          name: 'MedicineView',
          component: () => import('../views/Order/MedicineView.vue'),
        },
        {
          path: '/PrescriptionView',
          name: 'PrescriptionView',
          component: () => import('../views/Order/PrescriptionView.vue'),
        },
        {
          path: '/RefundView',
          name: 'RefundView',
          component: () => import('../views/Order/RefundView.vue'),
        },
        {
          path: '/RegisterView',
          name: 'RegisterView',
          component: () => import('../views/Order/RegisterView.vue'),
        },
        {
          path: '/CarouselList',
          name: 'CarouselList',
          component: () => import('../views/ContentManagement/CarouselList.vue'),
        },
        {
          path: '/HospitalList',
          name: 'HospitalList',
          component: () => import('../views/Hospital/HospitalList.vue'),
        },

        // 医生管理相关路由
        {
          path: '/NewDoctor/DoctorManage',
          name: 'DoctorManage',
          component: DoctorManage
        },
        {
          path: '/NewDoctor/DoctorAudit',
          name: 'DoctorAudit',
          component: DoctorAudit
        },
        {
          path: '/NewDoctor/ServiceAudit',
          name: 'ServiceAudit',
          component: ServiceAudit
        },
        
        // 退款申请路由
        {
          path: '/RefundManage',
          name: 'RefundManage',
          component: RefundManage
        },
        
        // 挂号单管理路由
        {
          path: '/RegisterManage',
          name: 'RegisterManage',
          component: RegisterManage
        },

        // 问诊单管理路由
        {
          path: '/ConsultationManage',
          name: 'ConsultationManage',
          component: ConsultationManage
        },
        {
          path: '/PrescriptionOrderManage',
          name: 'PrescriptionOrderManage',
          component: PrescriptionOrderManage
        },
        {
          path: '/MedicineOrderManage',
          name: 'MedicineOrderManage',
          component: MedicineOrderManage
        },
      ]
    },
  ],
})

// 全局前置导航守卫
router.beforeEach((to, from, next) => {
  // 登录页路径（根据你的实际路径调整）
  const loginPath = '/';
  // 假设token存储在localStorage
  const token = localStorage.getItem('token');
  if (to.path !== loginPath && !token) {
    // 未登录，强制跳转到登录页
    next(loginPath);
  } else {
    // 已登录或访问登录页，放行
    next();
  }
});

export default router
